package com.william.xiaobujiantest;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.Log;
import com.william.xiaobujiantest.Tools.DatetimeCalcTool;
import static android.content.ContentValues.TAG;
import static com.william.xiaobujiantest.Whats.RESTARTSERVICE;


/**
 * 天气时钟小部件
 */
public class WeatherClockWidget extends AppWidgetProvider {

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);

        if (intent.getAction().equals(Intent.ACTION_TIME_TICK)){
            String[] dateTimes = DatetimeCalcTool.sync(context);
            syncWeatherTask(context, dateTimes);
        }
        if (intent.getAction().equals(RESTARTSERVICE)){
            Log.e(TAG, "onReceive: 收到常驻服务已停止的F广播,将重启常驻服务！" );
            startMainService(context);
        }
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        //注册系统广播（时间自然流逝）接收器
        IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK);
        WeatherClockWidget receiver = new WeatherClockWidget();
        context.getApplicationContext().registerReceiver(receiver, filter);
        DatetimeCalcTool.sync(context);
        startSyncWeatherService(context);
        startMainService(context);

    }

    private  void startMainService(Context context){
        Intent intent = new Intent(context, MainService.class);
        context.startService(intent);
    }
    /**
     * 定时启动任务
     * @param dateTimes
     */
    private void syncWeatherTask(Context context,String[] dateTimes){
        String hhmm = dateTimes[3].substring(0,5);
        if(hhmm.equals(Whats.SYNCWEATHERTIME)){
            startSyncWeatherService(context);
        }
    }

    /**
     * 启动天气同步服务
     * @param context
     */
    private void startSyncWeatherService(Context context){

        Intent intent = new Intent(context, WeatherService.class);
        context.startService(intent);
    }

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
        Log.e(TAG, "onDisabled: 巍哥不好了，我被退出了！" );
        //当widget不可用（删除）时停止常驻服务
        Intent intent=new Intent(context, MainService.class);
        context.stopService(intent);
    }

    /**
     * 把文字改字体，再画成图片显示
     * @param context
     * @param content
     * @return
     */
    static Bitmap buildUpdate(Context context, String content){
        Bitmap myBitmap = Bitmap.createBitmap(190, 110, Bitmap.Config.ARGB_4444);
        Canvas myCanvas = new Canvas(myBitmap);
        Paint paint = new Paint();
        Typeface tf= Typeface.createFromAsset(context.getAssets(),"fonts/HelveticaNeue-UltraLigExt.otf");
        paint.setAntiAlias(true);
        paint.setAlpha(255);//取值范围为0~255，值越小越透明
        paint.setSubpixelText(true);
        paint.setTypeface(tf);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        paint.setTextSize(80);
        paint.setTextAlign(Paint.Align.CENTER);
        myCanvas.drawText(content, 100, 60, paint);
        return myBitmap;
    }
}

